package MRtest;

//import MRDemo.bean;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Map extends Mapper<LongWritable, Text, Text, Bean> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Bean>.Context context) throws IOException, InterruptedException {
            String str = value.toString();
            String[] strarr = str.split("\t");

            String username = strarr[1];
            String log_id_string =strarr[0];
            String time_string = strarr[strarr.length-2];
            String cost_string = strarr[strarr.length-1];

            //转换为long类型
            long log_id = Long.parseLong(log_id_string);
//            long time = Long.parseLong(time_string);
            long time = Long.parseLong(time_string.trim());

        //转换为时间
            long timeMil = time * 1000;
            Date date = new Date(timeMil);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formatDate = sdf.format(date);

            //转换为BigDecimal
            BigDecimal cost = new BigDecimal(cost_string);

            Bean bean = new Bean(username,formatDate,cost);

            Text Username = new Text();
            Username.set(username);

            context.write(Username,bean);


    }
}
